package com.atguigu.tingshu.task;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class Task {

    @Value("${server.port}")
    private Integer port ;

    @XxlJob(value = "testJobHandler")
    public void taskDemo01() {
        XxlJobHelper.log("taskDemo01任务开始执行了....") ;
        String jobParam = XxlJobHelper.getJobParam();       // 获取任务执行的时候的参数
        log.info("task....taskDemo01...方法执行了 ， port ----> {} " , port);
        XxlJobHelper.log("taskDemo01任务执行完毕了....") ;
    }

    @XxlJob(value = "testShareJobHandler")
    public void taskDemo02() {

        // 获取执行器实例的总数，获取当前执行器实例的索引
        int shardTotal = XxlJobHelper.getShardTotal();
        int shardIndex = XxlJobHelper.getShardIndex();

        List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        for(Integer id : ids) {
            if(id % shardTotal == shardIndex) {
                log.info("处理了一个任务，任务的id -----> {}" , id);
            }
        }

    }

}
